$(function(){
    document.getElementById("login").addEventListener("submit", function(event) {
event.preventDefault();
const username = document.getElementsByName("username")[0].value;
const password = document.getElementsByName("password")[0].value;
const regex = /^[\u4e00-\u9fa5a-zA-Z0-9_]{6,10}$/;

if (!regex.test(username)) {
    alert("用户名格式错误，请输入6-10位的字母、数字或下划线");
    return false;
}

if (password.length < 6 || password.length > 12) {
    alert("密码长度需为6-12位");
    return false;
}
});

document.getElementById("register").addEventListener("submit", function(event) {
event.preventDefault();//阻止表单的默认提交行为
const Cusername = document.getElementsByName("Cusername")[0].value;
const Cpassword = document.getElementsByName("Cpassword")[0].value;
const Cpassword2 = document.getElementsByName("Cpassword2")[0].value;
const regex = /^\d{6,10}$/;

if (!regex.test(Cusername)) {
    alert("用户名格式错误，请输入6-10位的数字");
    return false;
}

if (Cpassword.length < 6 || Cpassword.length > 12) {
    alert("密码长度需为6-12位");
    return false;
}

if (Cpassword !== Cpassword2) {
    alert("两次输入的密码不一致");
    return false;
}
});

//背景随鼠标移动
window.addEventListener('mousemove',function(e){

let x = e.clientX;
let y = e.clientY;//获取鼠标的当前位置（x和y坐标）
let width = window.innerWidth;
let height = window.innerHeight;//获取窗口的内部宽度和高度
let setX = -(x-width/2)*0.1;//根据鼠标的位置和窗口的大小，计算出一个偏移量
let setY = -(y-height/2)*0.1;
$('.box').css({
    "left": setX+"px",
    "top": setY+"px" 
})//据计算出的偏移量进行移动
})
//背景切换
let flag = 1;
$("#form-type").on('change',function(){
if(flag){
    $('.box').css({
        "background":"url(../images/banner-02.jpg) no-repeat",//flag为1，则将背景图片
        "background-size":"cover"
    })
    flag = 0;
}else{
    $('.box').css({
        "background":"url(../images/banner-01.jpg) no-repeat",
        "background-size":"cover"
    })
    flag = 1;
}
})

$('.form-input').on('click',function(){//元素被点击时，会触发另一个回调函数
$('.form-input').removeClass('form-error');
$('.form-input>.error-info').remove();//用户点击表单输入框时清除错误信息
})

})